package com.ruoyi.web.controller.sys.Management;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.BusiProjectItem;
import com.ruoyi.system.domain.PersonItem;
import com.ruoyi.system.service.BusiUserWorkhourService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.PersonItemService;
import com.ruoyi.system.service.ProjectItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * 子项目管理
 *
 * @author lyh
 */
@Api("子项目管理")
@RestController
@RequestMapping("/sys/Management/item")
public class ItemManagementController extends BaseController {
    @Autowired
    ProjectItemService projectItemService;

    @Autowired
    ISysUserService userService;

    @Autowired
    BusiUserWorkhourService busiUserWorkhourService;

    @Autowired
    PersonItemService personItemService;

    /**
     * 查询到所有的子项目
     *
     * @param projectId
     * @return
     */
    @ApiOperation("显示所有子项目")
    @PostMapping("/showItem/{projectId}")
    public TableDataInfo showItem(@ApiParam(name = "projectId", value = "传入项目编码的参数") @PathVariable String projectId) {
        startPage();
        return getDataTable(projectItemService.findByProjectId(projectId));
    }

    /**
     * 添加子项目
     *
     * @param busiProjectItem
     * @return
     */
    @ApiOperation("添加子项目")
    @PostMapping("/addItem")
    public AjaxResult addItem(
            @ApiParam(name = "busiProjectItem", value = "子项目信息") @Validated @RequestBody BusiProjectItem busiProjectItem
            , @ApiParam(name = "nameList", value = "参加人员列表") @RequestParam(value = "nameList", required = false) List<String> nameList) {

        String d = "";
        Calendar date = Calendar.getInstance();
        d = d + String.valueOf(date.get(Calendar.YEAR)) + "/";
        d = d + String.valueOf(date.get(Calendar.MONTH) + 1);
        BusiProjectItem byItemName = projectItemService.findByItemName(busiProjectItem.getItemName());
        String uuid = IdUtils.simpleUUID();
        busiProjectItem.setItemId(uuid);
        busiProjectItem.setPrdCompleteWorkhours(0);
        busiProjectItem.setPsoCompleteWorkhours(0);
        busiProjectItem.setSrdCompleteWorkhours(0);
        busiProjectItem.setOthersCompleteWorkhours(0);
        if (byItemName == null) {
            List<PersonItem> personItems = new ArrayList<>();
            //根据前台传输的list，查询到对应的name，并进行字符串的拼接
            for (String s : nameList) {
                PersonItem personItem = new PersonItem();
                SysUser sysUser = userService.selectUserByUserName(s);
                personItem.setUserName(s);
                personItem.setUserId(String.valueOf(sysUser.getUserId()));
                personItem.setItemId(uuid);
                personItem.setItemName(busiProjectItem.getItemName());
                personItem.setDate(d);
                personItems.add(personItem);
            }
            for (PersonItem personItem : personItems) {
                String uu = IdUtils.simpleUUID();
                personItem.setId(uu);
                personItemService.insert(personItem);
                System.out.println(personItem.toString());
            }
            return AjaxResult.success(projectItemService.insertItem(busiProjectItem));
        } else {
            return AjaxResult.error("增加子项目'" + byItemName.getItemName() + "'失败，子项目名字已存在");
        }
    }

    /**
     * 删除子项目
     */
    @ApiOperation("删除子项目")
    @PostMapping("/delete")
    public AjaxResult delete(@ApiParam(name = "busiProjectItem", value = "要删除的子项目") @Validated @RequestBody BusiProjectItem busiProjectItem) {
        return AjaxResult.success(projectItemService.deleteItem(busiProjectItem));
    }

    /**
     * 修改子项目
     */
    @ApiOperation("修改子项目")
    @PostMapping("update")
    public AjaxResult update(@ApiParam(name = "busiProjectItem", value = "传入要修改的子项目") @Validated @RequestBody BusiProjectItem busiProjectItem) {
        BusiProjectItem byItemId = projectItemService.findByItemId(busiProjectItem.getItemId());
        if (byItemId.getItemName().equals(busiProjectItem.getItemName())) {
            return AjaxResult.success(projectItemService.updateItem(busiProjectItem));
        } else {
            BusiProjectItem byItemName = projectItemService.findByItemName(busiProjectItem.getItemName());
            if (byItemName != null) {
                return AjaxResult.error("修改子项目名字为'" + byItemName.getItemName() + "'失败，子项目名字已存在");
            } else {
                return AjaxResult.success(projectItemService.updateItem(busiProjectItem));
            }
        }
    }

    /**
     * 子项目结束操作
     */
    @ApiOperation("子项目结束操作")
    @PostMapping("end")
    public AjaxResult end(@ApiParam(name = "busiProjectItem", value = "传入要结束的子项目") @Validated @RequestBody BusiProjectItem busiProjectItem) {
        busiProjectItem.setStatus(3);
        return AjaxResult.success(projectItemService.updateItem(busiProjectItem));
    }
}